using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	public static class RectTransformExtension
	{
		public static Vector2 GetProperSize(this RectTransform rectTransform)
		{
			Vector2 result = new Vector2(rectTransform.rect.width, rectTransform.rect.height);
			if (result.x == 0f && result.y == 0f)
			{
				LayoutElement component = rectTransform.GetComponent<LayoutElement>();
				if (component != null)
				{
					result.x = component.preferredWidth;
					result.y = component.preferredHeight;
				}
			}
			if (result.x == 0f && result.y == 0f)
			{
				LayoutGroup component2 = rectTransform.GetComponent<LayoutGroup>();
				if (component2 != null)
				{
					result.x = component2.preferredWidth;
					result.y = component2.preferredHeight;
				}
			}
			if (result.x == 0f && result.y == 0f)
			{
				result.x = LayoutUtility.GetPreferredWidth(rectTransform);
				result.y = LayoutUtility.GetPreferredHeight(rectTransform);
			}
			return result;
		}

		public static Vector3 GetPositionRegardlessOfPivot(this RectTransform rectTransform)
		{
			Vector3[] array = new Vector3[4];
			rectTransform.GetWorldCorners(array);
			return (array[0] + array[2]) / 2f;
		}

		public static Vector3 GetLocalPositionRegardlessOfPivot(this RectTransform rectTransform)
		{
			Vector3[] array = new Vector3[4];
			rectTransform.GetLocalCorners(array);
			return (array[0] + array[2]) / 2f;
		}

		public static void SetAnchorX(this RectTransform rectTransform, float min, float max)
		{
			Vector2 anchorMin = rectTransform.anchorMin;
			rectTransform.anchorMin = new Vector2(min, anchorMin.y);
			Vector2 anchorMax = rectTransform.anchorMax;
			rectTransform.anchorMax = new Vector2(max, anchorMax.y);
		}

		public static void SetAnchorY(this RectTransform rectTransform, float min, float max)
		{
			Vector2 anchorMin = rectTransform.anchorMin;
			rectTransform.anchorMin = new Vector2(anchorMin.x, min);
			Vector2 anchorMax = rectTransform.anchorMax;
			rectTransform.anchorMax = new Vector2(anchorMax.x, max);
		}
	}
}
